package org.jradweb.tools.generator.freemarker;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import org.apache.maven.plugin.logging.Log;
import org.jradweb.tools.generator.freemarker.util.Templates;
import org.jradweb.tools.generator.utils.LogImpl;
import org.jradweb.tools.generator.utils.StringUtils;

import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
 * Use freemarker template to create class file
 * @author Paul Wang
 */
public class ClassOperator extends FreemarkerOperator {
	private static final java.text.DateFormat DATA_FORMAT = new java.text.SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss.S");
	
	

	private String modelName;

	private Log log;

	public ClassOperator(File rootPath, Log log, String groupId,
			String artifactId, String modelName) {
		
		super(rootPath, log, groupId, artifactId);
		
		this.modelName = modelName;
		this.log=getLog();
		map = new HashMap<String, Object>();
		map.put("modelName", modelName);		
		map.put("lowcaseModelName", modelName.toLowerCase());	
		map.put("modelTableName", StringUtils.lowerCaseName(modelName));
		map.put("modelVariableName", StringUtils.lowerCaseFirst(modelName));
	}

	

	
	public class DaoVo{
		private String dao;
		private String lowerCaseFirstDao;
		public String getDao() {
			return dao;
		}
		public void setDao(String dao) {
			this.dao = dao;
		}
		public String getLowerCaseFirstDao() {
			return lowerCaseFirstDao;
		}
		public void setLowerCaseFirstDao(String lowerCaseFirstDao) {
			this.lowerCaseFirstDao = lowerCaseFirstDao;
		}
	}

	
	
	

	public void createModelFile() throws IOException, TemplateException {
		File srcPath = this.getSourcePath();

		File packagePath = new File(srcPath.getAbsolutePath() + "/"
				+ StringUtils.packageToPath(getGroupId()) + "/"
				+ getArtifactId() + "/model");

		if (!packagePath.exists()) {
			packagePath.mkdirs();
		}

		File modelFile = new File(packagePath.getAbsoluteFile() + "/"
				+ modelName + ".java");
		if (modelFile.exists()) {
			getLog().info(
					"Model class \"" + modelName + ".java\" already exists!");
			return;
		}
		modelFile.createNewFile();

		this.writeContent(modelFile, Templates.MODEL_TEMPLATE);
		log
				.info("Model class \"" + modelName
						+ ".java\" created successfully!");
	}

	public void createDaoFile() throws IOException, TemplateException {
		File srcPath = this.getSourcePath();

		File packagePath = new File(srcPath.getAbsolutePath() + "/"
				+ StringUtils.packageToPath(getGroupId()) + "/"
				+ getArtifactId() + "/dao");

		if (!packagePath.exists()) {
			packagePath.mkdirs();
		}

		File daoFile = new File(packagePath.getAbsoluteFile() + "/" + modelName
				+ "Dao.java");
		if (daoFile.exists()) {
			getLog().info(
					"Dao inteface \"" + modelName
							+ "Dao.java\" already exists!");
			return;
		}
		daoFile.createNewFile();

		this.writeContent(daoFile, Templates.DAO_TEMPLATE);
		log.info("Dao inteface \"" + modelName
				+ "Dao.java\" created successfully!");
	}

	public void createDaoImplFile() throws IOException, TemplateException {
		File srcPath = this.getSourcePath();

		File packagePath = new File(srcPath.getAbsolutePath() + "/"
				+ StringUtils.packageToPath(getGroupId()) + "/"
				+ getArtifactId() + "/dao/impl");

		if (!packagePath.exists()) {
			packagePath.mkdirs();
		}

		File daoFile = new File(packagePath.getAbsoluteFile() + "/" + modelName
				+ "DaoImpl.java");
		if (daoFile.exists()) {
			getLog().info(
					"DaoImpl class \"" + modelName
							+ "DaoImpl.java\" already exists!");
			return;
		}
		daoFile.createNewFile();

		this.writeContent(daoFile, Templates.DAO_IMPL_TEMPLATE);
		log.info("DaoImpl class\"" + modelName
				+ "DaoImpl.java\" created successfully!");
	}
	
	
	public void createServiceFile() throws IOException,TemplateException{
		File srcPath = this.getSourcePath();

		File packagePath = new File(srcPath.getAbsolutePath() + "/"
				+ StringUtils.packageToPath(getGroupId()) + "/"
				+ getArtifactId() + "/service/impl");

		if (!packagePath.exists()) {
			packagePath.mkdirs();
		}

		File daoFile = new File(packagePath.getAbsoluteFile() + "/" + modelName
				+ "DaoImpl.java");
		if (daoFile.exists()) {
			getLog().info(
					"DaoImpl class \"" + modelName
							+ "DaoImpl.java\" already exists!");
			return;
		}
		daoFile.createNewFile();

		this.writeContent(daoFile, Templates.DAO_IMPL_TEMPLATE);
		log.info("DaoImpl class\"" + modelName
				+ "DaoImpl.java\" created successfully!");
	}

	public void createDaoTestFile() throws IOException, TemplateException {
		File testPath = this.getTestSourcePath();

		File packagePath = new File(testPath.getAbsolutePath() + "/"
				+ StringUtils.packageToPath(getGroupId()) + "/"
				+ getArtifactId() + "/dao/test");

		if (!packagePath.exists()) {
			packagePath.mkdirs();
		}

		File daoFile = new File(packagePath.getAbsoluteFile() + "/" + modelName
				+ "DaoTest.java");
		if (daoFile.exists()) {
			getLog().info(
					"DaoTest class \"" + modelName
							+ "DaoTest.java\" already exists!");
			return;
		}
		daoFile.createNewFile();

		this.writeContent(daoFile, Templates.DAO_TEST_TEMPLATE);
		log.info("DaoTest class\"" + modelName
				+ "DaoTest.java\" created successfully!");
	}
	
	

	public void createDaoTestDataSetFile() throws IOException,
			TemplateException {
		File testResourcesPath = this.getTestResourcesPath();

		File packagePath = new File(testResourcesPath.getAbsolutePath() + "/"
				+ StringUtils.packageToPath(getGroupId()) + "/"
				+ getArtifactId() + "/dao/test");

		if (!packagePath.exists()) {
			packagePath.mkdirs();
		}

		File daoFile = new File(packagePath.getAbsoluteFile() + "/" + modelName
				+ "DaoTest.xml");
		if (daoFile.exists()) {
			getLog().info(
					"DaoTest dataset \"" + modelName
							+ "DaoTest.xml\" already exists!");
			return;
		}
		daoFile.createNewFile();

		this.writeContent(daoFile, Templates.DAO_TEST_DATASET_TEMPLATE);
		log.info("DaoTest dataset \"" + modelName
				+ "DaoTest.xml\" created successfully!");
	}

	protected void writeContent(File file, Template template) throws IOException,
			TemplateException {
		java.io.FileWriter writer = new FileWriter(file);
		template.process(map, writer);
		writer.close();
	}

	
	public static void main(String[] args) {
		ClassOperator opr = new ClassOperator(new File("./test/"),
				new LogImpl(ClassOperator.class), "com.infowarelab", "vms", "Service");

		try {
			opr.createModelFile();
			opr.createDaoFile();
			opr.createDaoImplFile();
			opr.createDaoTestFile();
			opr.createDaoTestDataSetFile();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
